package com.quanxiaoha.weblog.admin.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.quanxiaoha.weblog.admin.constant.RedisKeyConstants;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private ObjectMapper objectMapper;
    @Override
    public List<String> getPermissionList(Object o, String s) {
        return List.of();
    }

    @SneakyThrows
    @Override
    public List<String> getRoleList(Object o, String s) {
        String roleName = StpUtil.getLoginIdAsString();

        List roles = new ArrayList<>();
        if(StringUtils.isNoneBlank(roleName)) {
            if(Objects.equals(roleName, "admin")) {
                roles.add("ROLE_ADMIN");
            } else if(Objects.equals(roleName, "test")) {
                roles.add("ROLE_VISITOR");
            }
        }
        return roles;
//        String userRolesValue = redisTemplate.opsForValue().get(RedisKeyConstants.buildUserRolesKey(roleName));
//
//        if (StringUtils.isBlank(userRolesValue)) {
//            return null;
//        }
//        // 将 JSON 字符串转换为 List<String> 集合
//        return objectMapper.readValue(userRolesValue, new TypeReference<>() {});

    }
}
